package components;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class AddValueDialog extends JDialog implements ActionListener,
		ItemListener {

	private final JPanel contentPanel = new JPanel();

	private JLabel lblPointHasBeen;
	private JTextField valueNameField;
	private JComboBox<String> lingVariables, lingValues;
	ArrayList<LinguisticVariable> variables;
	JButton add, cancel, delete;

	/**
	 * Create the dialog.
	 */
	public AddValueDialog(ArrayList<LinguisticVariable> variables) {
		this.variables = variables; // set the current variable list
		setBounds(100, 100, 450, 300); // set location and size of the dialog box
		getContentPane().setLayout(new BorderLayout()); // set borderLayout
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); // set the border of the main contentPanel of dialogbox
		getContentPane().add(contentPanel, BorderLayout.CENTER); // add contentPanel to center of dialog
		contentPanel.setLayout(null); // setLayout of contentPanel to null

		{
			lingVariables = new JComboBox(); // combobox for variables
			lingVariables.setBounds(223, 40, 109, 27); // set location and size of combobox variables
			for (int variableIndex = 0; variableIndex < variables.size(); variableIndex++) {
				// get all the variables in the variable list into the combobox
				lingVariables.addItem(variables.get(variableIndex).getName());

			}
			contentPanel.add(lingVariables); // add combobox to jdialog screen
		}
		{
			JLabel lblLinguisticValue = new JLabel("Select Variable:"); // label the value combobox
			lblLinguisticValue.setBounds(110, 46, 200, 21); // set location and size of label
			contentPanel.add(lblLinguisticValue); // add label to main screen of dialog box
		}

		{
			JLabel lblX = new JLabel("Value Name:"); // label the text field for value
			lblX.setBounds(110, 99, 100, 15); // set location and size
			contentPanel.add(lblX); // add to dialog box screen
		}
		valueNameField = new JTextField(); // create text field for values
		valueNameField.setBounds(223, 96, 150, 21); // set location and size
		contentPanel.add(valueNameField); // add it to content panel 

		{
			lblPointHasBeen = new JLabel("value has been added"); // creating success label
			lblPointHasBeen.setVisible(false); // do not show it for now
			lblPointHasBeen.setBounds(94, 204, 196, 15); // set location and size 
			contentPanel.add(lblPointHasBeen); // add label to the screen of dialog box
		}
		{
			JPanel buttonPane = new JPanel(); // creating panel for holding add and cancel button
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); // setLayout to flowlayout to align the elements
			getContentPane().add(buttonPane, BorderLayout.SOUTH); // add buttonpanel to south location inside of dialog box
			{
				add = new JButton("Add"); // create add button
				add.addActionListener(this); // add click event listener
				add.setActionCommand("OK");
				buttonPane.add(add); // add to buttonPane panel to show on dialog screen
				getRootPane().setDefaultButton(add); // set this button as default
			}
			{
				cancel = new JButton("Cancel"); // create cancelbutton
				cancel.addActionListener(this); // add click Listener to cancel button
				cancel.setActionCommand("Cancel");
				cancel.addActionListener(new ActionListener() {
						// called when cancel button is clicked
					@Override
					public void actionPerformed(ActionEvent arg0) {
						
						lblPointHasBeen.setVisible(false);// hide the success label
						valueNameField.setText(""); // empty the value text field

					}
				});
				buttonPane.add(cancel); // add cancel button to dialog's button panel
			}
		}
	}

	public AddValueDialog(final ArrayList<LinguisticVariable> variables,
			boolean isDeleting) {
		 // this constructor is used to generate the delete value dialog
		this.variables = variables; // sets the current list of variables
		setBounds(100, 100, 450, 300); // sets location and bounds of dialog box
		getContentPane().setLayout(new BorderLayout()); // set layout to border layout
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); // add border to contentpanel
		getContentPane().add(contentPanel, BorderLayout.CENTER); // align contentpanel iside dialog box
		contentPanel.setLayout(null); // set layout to null

		{
			lingVariables = new JComboBox(); // create variables combobox
			lingVariables.setBounds(223, 40, 109, 27); // set location and size of variable combobox
			for (int variableIndex = 0; variableIndex < variables.size(); variableIndex++) {
				// add all variables from list to variable combobox
				lingVariables.addItem(variables.get(variableIndex).getName());

			}
			lingVariables.setSelectedIndex(-1); // set no item selected
			lingVariables.addItemListener(this); // add click event listener
			contentPanel.add(lingVariables); // add this combobox to content panel inside of dialog box
		}
		{
			JLabel lblLinguisticValue = new JLabel("Select Variable:"); // label the variable combobox
			lblLinguisticValue.setBounds(110, 46, 200, 21); // set location and size of the label
			contentPanel.add(lblLinguisticValue);// add the label to dialog box
		}

		{
			JLabel lblX = new JLabel("Value Name:"); // label the value textField
			lblX.setBounds(110, 99, 100, 15);// set location and size of label 
			contentPanel.add(lblX);// add label to screen
		}
		lingValues = new JComboBox(); // create a value combobox
		lingValues.setBounds(223, 96, 150, 21); // set its location and size
		contentPanel.add(lingValues); // add the value combobox to main dialog screen

		{
			lblPointHasBeen = new JLabel("value has been deleted"); // success label
			lblPointHasBeen.setVisible(false);// dont show it for now
			lblPointHasBeen.setBounds(94, 204, 196, 15);// set location and size 
			contentPanel.add(lblPointHasBeen);// add it to dialog
		}
		{
			JPanel buttonPane = new JPanel(); // create buttonPanel
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));// set its layout 
			getContentPane().add(buttonPane, BorderLayout.SOUTH);// align contentPanel to south area inside of dialog
			{
				delete = new JButton("Delete"); // create a delete button
				delete.addActionListener(this); // add click event on delete button
				delete.setActionCommand("OK");// set action command
				buttonPane.add(delete); // add button to button pane inside dialog
				getRootPane().setDefaultButton(delete);// set it as default
			}
			{
				cancel = new JButton("Cancel"); // create a cancel button
				cancel.addActionListener(this); // add event on click of the button
				cancel.setActionCommand("Cancel"); // sets action command
				cancel.addActionListener(new ActionListener() {
						// called when cancel button is clicked
					@Override
					public void actionPerformed(ActionEvent arg0) {
						lblPointHasBeen.setVisible(false); // hide the success lable
						lingValues.setSelectedIndex(-1);// set no value selected

					}
				});
				buttonPane.add(cancel); // add cancel button to button panel in dialog
			}
		}
	}

	public void setSelection(ArrayList<LinguisticVariable> variables) {

		for (LinguisticVariable variable : variables) {
			lingVariables.addItem(variable.getName());
		}
		lingVariables.setSelectedIndex(0);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
		
		if (event.getSource().equals(add)) {
			// this is called whene add btn is clicked
			if (lingVariables.getSelectedIndex() > -1
					&& (valueNameField.getText() != null && !valueNameField
							.getText().equals(""))) {
					// checks if the user has chosen a specific variable to add values to or not
				for (int variableIndex = 0; variableIndex < variables.size(); variableIndex++) {
					// loop through all variables in the list
					if (variables
							.get(variableIndex)
							.getName()
							.equalsIgnoreCase(
									lingVariables.getSelectedItem().toString())) {
						// get specific variable selected and fetch all values inside into a combobox
						
						variables.get(variableIndex).setValueNames(
								valueNameField.getText());// add values to chosen variable
						
						lblPointHasBeen.setVisible(true); //set the success label to show on screen
						JOptionPane.showMessageDialog(null, lblPointHasBeen.getText()); // success add message
						setVisible(false); // make dialog box disappear
						break; // stop looping
					}

				}
				// System.out.println(variables.get(variableIndex));
			}

		} else if (event.getSource().equals(delete)) {
			// when user clicks delete button after selecting value and variable for comboboxes
			if (lingVariables.getSelectedIndex() > -1
					&& (lingValues.getSelectedIndex() > -1)) {
					// check if user has selected values from the comboboxes
				for (int variableIndex = 0; variableIndex < variables.size(); variableIndex++) {
					if (variables
							.get(variableIndex)
							.getName()
							.equalsIgnoreCase(
									lingVariables.getSelectedItem().toString())) {

						variables.get(variableIndex).removeValueNames(
								lingValues.getSelectedItem().toString());// remove the selected values from the selected variable
						
						lblPointHasBeen.setVisible(true); // show success delete label in screen
						JOptionPane.showMessageDialog(null, lblPointHasBeen.getText()); // show success delete dialog on screen
						setVisible(false); // make dialog disappear
						break; // stop looping
					}

				}
				// System.out.println(variables.get(variableIndex));
			}

		}
	}

	@Override
	public void itemStateChanged(ItemEvent event) {

		if (event.getSource().equals(lingVariables)) {
			 // called when we choose a specific variable from combobox
			if (lingVariables.getSelectedIndex() > -1) {
				lingValues.removeAllItems(); // remove already existing values in combobox
				// as user selects the variable fetch values inside it
				String item = lingVariables.getSelectedItem().toString();
				for (int indexv = 0; indexv < variables.size(); indexv++) {
					if (item.equalsIgnoreCase(variables.get(indexv).getName())) {

						ArrayList<String> list = variables.get(indexv)
								.getValueNames(); // get the current selected variable name from the list
						for (int indexva = 0; indexva < list.size(); indexva++) {
							lingValues.addItem(list.get(indexva)); // add new values to values combobox based on chosen variable name

						}
					}

				}

			}
		}
	}
}
